import requests
import json
import time  # 新增时间控制模块


def call_analyse_sql(question: str, sql_result: str):
    url = "http://localhost:3001/analyse_sql"
    # url = "http://10.1.161.53:3001/analyse_sql"
    headers = {"Content-Type": "application/json"}
    data = {
        "question": question,
        "sql_result": sql_result
    }

    try:
        with requests.post(url, headers=headers, json=data, stream=True) as response:
            response.raise_for_status()
            buffer = []  # 新增缓冲区存储累积内容
            for line in response.iter_lines():
                if line:
                    decoded_line = line.decode('utf-8').strip()
                    if decoded_line.startswith("data:"):
                        content = decoded_line[len("data:"):].strip()
                        if content == "[DONE]":
                            print("\n分析完成")  # 保持换行效果
                            break
                        try:
                            json_data = json.loads(content)
                            new_text = json_data.get('content', '')

                            # 逐字符输出新内容
                            for char in new_text:
                                print(char, end='', flush=True)
                                time.sleep(0.1)  # 控制每个字符的显示间隔

                        except json.JSONDecodeError:
                            print("非结构化内容:", content)
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")

# ===== 使用示例（需替换为完整sql_result数据） =====
question = "江海货368 2025年2月20日航行记录"
sql_result = "[{\"bayonetName\":\"杭申线卡口\",\"boatName\":\"江海货368\",\"code\":\"hs899912\",\"direction\":\"入沪\",\"endPoint\":\"-43720.947,-30818.384\",\"imagePaths\":\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P0_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P1_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P2_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P3_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P4_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P5_20250220_104230.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P6_20250220_104230.jpg;\",\"imageUrls\":[\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P0_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P1_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P2_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P3_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P4_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P5_20250220_104230.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/10/P6_20250220_104230.jpg\"],\"load\":\"空载\",\"locationMapx\":-43720.947,\"locationMapy\":-30818.384,\"passTime\":\"2025-02-20 10:42:30.0\",\"speed\":3.69},{\"bayonetName\":\"黄浦江(松浦大桥南岸)\",\"boatName\":\"江海货368\",\"code\":\"5B16DC01A68A4D42A002C7A2F2A52192\",\"direction\":\"出沪\",\"endPoint\":\"-14860.154,-29250.958\",\"imagePaths\":\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P0_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P1_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P2_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P3_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P4_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P5_20250220_131544.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P6_20250220_131544.jpg;\",\"imageUrls\":[\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P0_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P1_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P2_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P3_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P4_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P5_20250220_131544.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/13/P6_20250220_131544.jpg\"],\"load\":\"空载\",\"locationMapx\":-14860.154,\"locationMapy\":-29250.958,\"passTime\":\"2025-02-20 13:15:44.0\",\"speed\":7.05},{\"bayonetName\":\"黄浦江(松浦大桥南岸)\",\"boatName\":\"江海货368\",\"code\":\"6F3D7264A98D4A98BAE69ED4360AC62F\",\"direction\":\"入沪\",\"endPoint\":\"-14860.154,-29250.958\",\"imagePaths\":\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P0_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P1_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P2_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P3_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P4_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P5_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P6_20250220_180620.jpg;/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P7_20250220_180620.jpg;\",\"imageUrls\":[\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P0_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P1_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P2_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P3_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P4_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P5_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P6_20250220_180620.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/8/2025/20250220/18/P7_20250220_180620.jpg\"],\"load\":\"重载\",\"locationMapx\":-14860.154,\"locationMapy\":-29250.958,\"passTime\":\"2025-02-20 18:06:20.0\",\"speed\":4.76},{\"bayonetName\":\"杭申线卡口\",\"boatName\":\"江海货368\",\"code\":\"hs900142\",\"direction\":\"出沪\",\"endPoint\":\"-43720.947,-30818.384\",\"imagePaths\":\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P0_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P1_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P2_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P3_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P4_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P5_20250220_214525.jpg;/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P6_20250220_214525.jpg;\",\"imageUrls\":[\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P0_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P1_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P2_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P3_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P4_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P5_20250220_214525.jpg\",\"/data/dataShareFolder/bayonetPhoto/origin/1/2025/20250220/21/P6_20250220_214525.jpg\"],\"load\":\"重载\",\"locationMapx\":-43720.947,\"locationMapy\":-30818.384,\"passTime\":\"2025-02-20 21:45:25.0\",\"speed\":3.69}]"

call_analyse_sql(question, sql_result)